package com.ssamkj.wstar.database;

//import org.junit.runner.RunWith;
//import org.junit.runners.Suite;
//import org.junit.runners.Suite.SuiteClasses;
//
//import static org.junit.Assert.assertEquals;
//
//import org.junit.AfterClass;
//import org.junit.BeforeClass;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.*;
import org.xml.sax.SAXException;
//import org.w3c.dom.Document;
//import org.w3c.dom.NodeList;

//import org.apache.crimson.tree.XmlDocument;
import org.junit.Test;

import com.ssamkj.wstar.dto.godMode.LocaleDTO;

public class DataBaseXML {
	private static String RESOURCE = "c:\\work\\W-StarNews\\workspace\\W-Stars\\src\\test\\resources\\database";

	enum NATION_LIST {
		MYID("myid"), NAME("name"), ACTIVE("active");

		private String name;

		NATION_LIST(String name) {
			this.name = name;
		}

		public String getName() {
			return this.name;
		}
	}

//	@Test
	public void main() {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\NATION_LIST.xml");
			Document doc = builder.parse(f);

//			Text us = doc.createTextNode("US");
//			Node myid = doc.createElement("myid");
//			myid.appendChild(us);
//
//			Text t2 = doc.createTextNode("미국");
//			Node name = doc.createElement("name");
//			name.appendChild(t2);
//
//			Text val3 = doc.createTextNode("true");
//			Node active = doc.createElement("active");
//			active.appendChild(val3);
//
//			Node item = doc.createElement("item");
//			item.appendChild(myid);
//			item.appendChild(name);
//			item.appendChild(active);
//			doc.getDocumentElement().appendChild(item);

			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("item");

			int length = list.getLength();
			for (int i = 0; i < length; i++) {
				Node node = list.item(i);
				System.out.println(node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element headLineElement = (Element) node;

					for (NATION_LIST nation : NATION_LIST.values()) {
						String tagName = nation.getName().toLowerCase();
						NodeList nameElement = headLineElement
								.getElementsByTagName(tagName);
						Element subItem = (Element) nameElement.item(0);
						NodeList subElement1 = subItem.getChildNodes();
						System.out.println("key = " + tagName + "\tvalue = "
								+ subElement1.item(0).getNodeValue());
					}
				}
			}
			saveFile(doc);

			// xDom.appendChild(item);

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void main2(){
		List<LocaleDTO> locale = new ArrayList<LocaleDTO>();
		LocaleDTO dto = new LocaleDTO();
		dto.setLoc("US");
		dto.setName("미국");
		dto.setActive(true);
		insertNation(dto);
		
		readNationList(locale);
		for(LocaleDTO l :locale){
			System.out.println(l);
		}
	}

	public void insertNation(LocaleDTO dto){
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\NATION_LIST.xml");
			Document doc = builder.parse(f);

			Text val1 = doc.createTextNode(dto.getLoc());
			Node myid = doc.createElement("myid");
			myid.appendChild(val1);

			Text val2 = doc.createTextNode(dto.getName());
			Node name = doc.createElement("name");
			name.appendChild(val2);

			Text val3 = doc.createTextNode(dto.isActive()?"true":"false");
			Node active = doc.createElement("active");
			active.appendChild(val3);

			Node item = doc.createElement("item");
			item.appendChild(myid);
			item.appendChild(name);
			item.appendChild(active);
			doc.getDocumentElement().appendChild(item);

			doc.getDocumentElement().normalize();
			saveFile(doc);

			// xDom.appendChild(item);

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void readNationList(List<LocaleDTO> locale){
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(RESOURCE + "\\NATION_LIST.xml");
			Document doc = builder.parse(f);


			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("item");

			int length = list.getLength();
			for (int i = 0; i < length; i++) {
				LocaleDTO dto = new LocaleDTO();
				Node node = list.item(i);
				System.out.println(node.getNodeName());

				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element headLineElement = (Element) node;

					for (NATION_LIST nation : NATION_LIST.values()) {
						String tagName = nation.getName().toLowerCase();
						NodeList nameElement = headLineElement
								.getElementsByTagName(tagName);
						Element subItem = (Element) nameElement.item(0);
						NodeList subElement1 = subItem.getChildNodes();
						System.out.println("key = " + tagName + "\tvalue = "
								+ subElement1.item(0).getNodeValue());
						
						switch(nation){
						case MYID:{
							dto.setLoc(subElement1.item(0).getNodeValue());
							break;
						}
						case NAME :{
							dto.setName(subElement1.item(0).getNodeValue());
							break;
						}
						case ACTIVE :{
							dto.setActive(subElement1.item(0).getNodeValue().equals("true"));
						}
						}
						
					}
					locale.add(dto);
				}
			}
//			saveFile(doc);

			// xDom.appendChild(item);

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
//		catch (TransformerException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
	
	
	protected boolean saveFile(Document doc) throws TransformerException {
		// 변환기 공장 생성
		TransformerFactory factory = TransformerFactory.newInstance();
		// 변환기 생성
		javax.xml.transform.Transformer transformer = factory.newTransformer();
		// XML 선언과 문서 유형 선언 내용 설정
		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		// DOMSource 객체 생성
		DOMSource source = new DOMSource(doc);
		// StreamResult 객체 생성
		StreamResult result = new StreamResult(new File(RESOURCE
				+ "\\NATION_LIST.xml"));
		// 파일로 저장하기
		transformer.transform(source, result);
		System.out.println("저장 완료");
		
		
//		TransformerFactory transformerFactory = TransformerFactory.newInstance();
//		Transformer transformer = transformerFactory.newTransformer();
//		DOMSource source = new DOMSource(doc);
////		StreamResult result = new StreamResult(new File(RESOURCE
////				+ "\\NATION_LIST.xml"));
//		StreamResult result = new StreamResult(new File("d:"
//				+ "\\NATION_LIST.xml"));
// 
//		// Output to console for testing
//		// StreamResult result = new StreamResult(System.out);
// 
//		transformer.transform(source, result);
		
		return false;
	}

	protected String printText(CharacterData node) {
		return node.getData();
	}
}
